<template>
  <div>
    <el-card class="box-card">
      <div>
        <div>
          <el-input placeholder="请输入学生名称" v-model="parms.studentName" size="mini" clearable style="width: 250px;margin-right:20px" />
          <el-input placeholder="请输入学号" v-model="parms.studentCode" size="mini" clearable style="width: 250px;margin-right:20px" />
          <el-input placeholder="请输入手机号" v-model="parms.phone" type="number" size="mini" clearable style="width: 250px;margin-right:20px" />
          <el-button size="mini" type="primary" @click="serch()" icon="el-icon-search">搜索</el-button>
          <el-button size="mini" type="primary" @click="add()" icon="el-icon-plus">新增</el-button>
          <el-button size="mini" type="success" @click="iport()" icon="el-icon-upload2">导入</el-button>
          <el-button size="mini" type="danger" @click="eport()" icon="el-icon-download">导出</el-button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 20px">
      <el-table :data="tableData" border="" style="width: 100%" @selection-change="handleSelectionChange" v-loading="loading">
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope" >
            <el-button size="mini" type="text" @click="editor(scope.row.studentId)">修改</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.studentId)" style="color: red">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="studentName" label="姓名" align="center" />
        <el-table-column prop="studentCode" label="学号" align="center" />
        <el-table-column prop="address" label="性别" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.sex == 0">男</span>
            <span v-else>女</span>
          </template>
        </el-table-column>
        <el-table-column prop="schoolName" label="所属学校" align="center" />
        <el-table-column prop="className" label="所属班级" align="center" />
        <el-table-column prop="phonenumber" label="手机号" align="center" />
        <el-table-column label="状态" align="center" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.deleteFlag == 0">正常</span>
            <span v-if="scope.row.deleteFlag == 1">未认证</span>
            <span v-if="scope.row.deleteFlag == 2">禁用</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="parms.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="parms.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="新增" :visible.sync="dialogVisible" width="40%">
      <Add @handCancle="handCancle"></Add>
    </el-dialog>
    <!-- <el-dialog title="发布任务" :visible.sync="dialogVisible1" width="50%">
      <Release @handCancle="handCancle"></Release>
    </el-dialog> -->
    <el-dialog title="任务详情" :visible.sync="dialogVisible2" width="40%">
      <Editor @handCancle="handCancle" :id='id' :showDialog="dialogVisible2"></Editor>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!-- <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据 -->
          <!-- <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link> -->
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, delFlag, exportStu } from "@/api/student/student"
import { getToken } from "@/utils/auth";
import Add from './add.vue'
import Editor from './editor.vue'
export default {
  components: {
    Add,
    Editor
  },
  data() {
    return {
      input: '',
      options: '',
      tableData: [1, 2],
      switch1: true,
      dialogVisible: false,
      dialogVisible2: false,
      value: 10,
      parms: {
        pageNum: 1,
        pageSize: 5,
        studentName: '',
        phone: null,
        studentCode: null,
        classId: null
      },
      id: null,
      total: null,
      query: {
        studentName: "",
        studentCode: "",
        phonenumber: "",
        classId: '',
        delFlag: ''
      },
      loading: false,
      exportLoading: false,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        // url: process.env.VUE_APP_BASE_API + "/web/commonController/upload"
        url: process.env.VUE_APP_BASE_API + '/web/studentInfo/import'
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    eport() {
      // var dateTime = new Date().getTime();
      // console.log(dateTime);
      // eport(this.query.studentName, this.query.studentCode, this.query.phonenumber, this.query.classId, this.query.delFlag).then((res) => {
      //   console.log(res.data)
      //   let blob = new Blob([res], {
      //       type: "application/vnd.ms-excel"
      //   });
      //   let url = window.URL.createObjectURL(blob);
      //   console.log(blob)
      //   var a = document.createElement("a");
      //   document.body.appendChild(a);
      //   a.href = url;
      //   a.download = "学生信息_" + dateTime + ".xls";
      //   a.click();
      //   window.URL.revokeObjectURL(url);
      // }).catch((error) => console.log(error));
      const queryParams = this.query;
      this.$confirm('是否确认导出所有学生数据项?', "提示", {
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportStu(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    del(id) {
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        delFlag(id).then(res => {
          if(res.code === 200){
            this.$message.success('删除成功')
            this.getList()
          }
          
        })
      }).catch(() => {});
    },
    editor(id) {
      this.id = id
      this.dialogVisible2 = true
    },
    serch() {
      this.getList()
    },
    add() {
      this.dialogVisible = true
    },
    getList() {
      this.loading = true
      getList(this.parms.pageNum, this.parms.pageSize, this.parms.studentName, this.parms.phone, this.parms.studentCode, this.parms.classId).then(res => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    release() {
      this.dialogVisible1 = true
    },
    handCancle(e) {
      if(e === 1){
        this.getList()
      }
      this.dialogVisible = false
      this.dialogVisible2 = false
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.parms.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.parms.pageNum = val
      this.getList()
    },
    /** 导入按钮操作 */
    iport() {
      this.upload.title = "导入信息";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // console.log(response.data.fileUrl)
      // iport(response).then( res => {
      //   console.log(res)
      // })
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
}
</script>

<style>
.flex{
  display: flex;
  justify-content: space-between;
}
.el-table th.gutter {
  display: table-cell !important
}
</style>
